package com.cacho.s2b.lesson.learn;

import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Objects;

/**
 * @Description 路径相关
 * @Author LingWei
 * @date 2023/12/17
 **/
public class LearnPath {
    public static void main (String[] args){
        LearnPath path = new LearnPath();
        path.initFile ();
    }

    public void initFile (){
        System.out.println("类的位置："+getClass());
        System.out.println("当前类的位置："+this.getClass());
        try {
            InputStream in1 = this.getClass().getResourceAsStream("data/config.ini");
            InputStream in2 = this.getClass().getResourceAsStream("/data/config.ini");
            InputStream in3 = this.getClass().getClassLoader().getResourceAsStream("data/config.ini");
            FileInputStream in4 = new FileInputStream("F:\\JavaProject\\config.ini") ;
            System.out.println("会从类目录下找，必须有/：" + in1); // null
            System.out.println("会从编译后的整个class目录下找：" + in2);
            System.out.println("getClassLoader会从整个class目录下找：" + in3);
            System.out.println("指定目录查找：" + in4);

            String defCsv = "data/def/api_def.csv";
            String mappingFolder = defCsv.substring(0,defCsv.lastIndexOf("/")+1) +"mappingpath";
            System.out.println("mappingpath目录：" + mappingFolder);
            // getResource("").getPath() 获取当前绝对目录 /F:/JavaProject/java-lesson/target/classes/com/cacho/s2b/lesson/learn/
            // getResource("/").getPath() 获取当前相对目录 /F:/JavaProject/java-lesson/target/classes/data/def/mappingpath
            String fullPath = this.getClass().getResource("/")+ mappingFolder;
            System.out.println("fullPath目录：" + fullPath);
            System.out.println("/classes/com/cacho/s2b/lesson/learn/：" + this.getClass().getResource("")); // 从类目录下找
            System.out.println("/classes/：" + this.getClass().getResource("/")); // 指定从class目录找
            System.out.println("null：" + this.getClass().getClassLoader().getResource("/")); // 会自动从class目录找，不需要/
            System.out.println("/classes/：" + this.getClass().getClassLoader().getResource("")); // 自动从class目录找

        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
